﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
    public partial class UnosOcjene : Form
    {
        int odabraniPredmet;
        int odabraniRazred;
        String odabraniUcenik, JMBG;
        List<String> sifre;
        MySqlConnection conn;
        MySqlDataReader rdr = null;
        public UnosOcjene(String maticni, int predmet, int razred, MySqlConnection c)
        {
            InitializeComponent();
            odabraniPredmet = predmet;
            odabraniRazred = razred;
            conn = c;
            JMBG = maticni;
            sifre = new List<String>();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            maskedTextBox1.Clear();
            errorProvider1.Clear();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (maskedTextBox1.Text == "") errorProvider1.SetError(maskedTextBox1, "Niste unijeli ocjenu!");
            if (errorProvider1.GetError(maskedTextBox1)!="")
            {
                MessageBox.Show(errorProvider1.GetError(maskedTextBox1), "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (comboBox1.Text == "")
                MessageBox.Show("Niste odabrali tip provjere", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                int ocjena = Convert.ToInt32(maskedTextBox1.Text);
                DateTime datum = Convert.ToDateTime(dateTimePicker1.Text);
                String tip = comboBox1.Text;
                VerifikacijaN nova = new VerifikacijaN(JMBG, conn, odabraniPredmet, textBox3.Text, ocjena, datum, tip);
                nova.Show();
            }
        }

        private void maskedTextBox1_Validating(object sender, CancelEventArgs e)
        {
            if (maskedTextBox1.Text == "")
            {
                errorProvider1.SetError(maskedTextBox1, "Niste unijeli ocjenu");
                e.Cancel = true;
                errorProvider1.SetError(maskedTextBox1, "Niste unijeli ocjenu");
            }
            else if (Convert.ToInt32(maskedTextBox1.Text) < 1 || Convert.ToInt32(maskedTextBox1.Text) > 5)
            {
                errorProvider1.SetError(maskedTextBox1, "Pogresna ocjena");
                e.Cancel = true;
                errorProvider1.SetError(maskedTextBox1, "Pogresna ocjena");
            }
            else
            {
                errorProvider1.Clear();
            }
            
        }


        private void Form10_Load(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button6.Enabled = false;
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Ucenik WHERE Razred = " + odabraniRazred + ";", conn);
            try
            {
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    listBox1.Items.Add(Convert.ToString(rdr["Ime"]) + " " + Convert.ToString(rdr["Prezime"]));
                    sifre.Add(Convert.ToString(rdr["JMBG"]));
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int indeks = listBox1.SelectedIndex;
            odabraniUcenik = sifre[indeks];
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Ucenik WHERE JMBG = " + odabraniUcenik + ";", conn);
            try
            {
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    textBox1.Text = Convert.ToString(rdr["Ime"]);
                    textBox2.Text = Convert.ToString(rdr["Prezime"]);
                    textBox3.Text = Convert.ToString(rdr["JMBG"]);
                    pictureBox1.ImageLocation = Convert.ToString(rdr["URLSlike"]);
                    button6.Enabled = true;
                    button1.Enabled = true;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
        }
    }
}
